package com.wei.shopping.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.protocol.x.Notice;
import com.wei.shopping.dao.*;
import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.pojo.Collect;
import com.wei.shopping.pojo.Commodity;
import com.wei.shopping.pojo.Notices;
import com.wei.shopping.pojo.UserInfo;
import com.wei.shopping.service.CollectService;
import com.wei.shopping.utils.KeyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author Wei
 * @Date 2021/4/29 14:12
 */
@Service
@Transactional
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private CommMapper commMapper;
    @Resource
    private NoticesMapper noticesMapper;
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private UserMapper userMapper;
    /**
     * 删除收藏
     * @param colid
     */
    @Override
    public void delcollect(String colid){
        collectMapper.delcollect(colid);
    }

    /**
     * 分页查询收藏列表
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult collectFindAll(QueryPageBean queryPageBean,String collectuserid){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Collect> pages = collectMapper.collectFindAll(queryPageBean.getQueryString(),collectuserid );
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 收藏商品
     * @param commid
     * @param userid
     */
    @Override
    public void collect(String commid,String userid){
        //查找商品信息和用户信息
        Commodity comm = commMapper.findById(commid);
        UserInfo user = userMapper.findById(userid);
        Collect collect = new Collect();
        collect.setId(KeyUtil.genUniqueKey());
        collect.setCommid(commid);
        collect.setCommdesc(comm.getCommdesc());
        collect.setCommname(comm.getCommname());
        collect.setCommuserid(comm.getUserid());
        collect.setUsername(comm.getSchool());
        collect.setUsername(user.getUsername());
        collect.setCollectuserid(userid);
        collectMapper.addcollect(collect);
        //添加通知
        Notices notice = new Notices();
        notice.setId(KeyUtil.genUniqueKey());
        notice.setUserid(comm.getUserid());
        notice.setTpname("商品收藏");
        notice.setWhys("你的商品："+comm.getCommname()+"被收藏了");
        noticesMapper.addnotices(notice);
    }

    /**
     * 取消收藏
     * @param commid
     * @param userid
     */
    @Override
    public void cancelcollect(String commid,String userid){
        collectMapper.cancelcollect(commid,userid);
    }
    /**
     * 通过commid和userid查找收藏
     * @param commid
     * @param userid
     * @return
     */
    @Override
    public Collect findByCommidAndUserid(String commid,String userid){
        Collect collect = collectMapper.findByCommidAndUserid(commid, userid);
        return collect;
    }
}
